import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:funtv/funtv.dart';
import 'package:video_player/video_player.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String videoUrl = '';
  VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await Funtv.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
    print("isInit:");
    print( await Funtv.isInit);
    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'), // $_platformVersion
        ),
        body: Column(
          children: [
            Container(
              height: 300,
              child: videoUrl == '' ? Text("loading") : Center(
                child: _controller.value.initialized
                    ? AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                )
                    : Container(),
              ),
            ),
            ElevatedButton(onPressed: () async{
              try {
                videoUrl = await Funtv.getVideoUrl({
                  "cmCode":"M0DBMBYKBE",
                  "ceCode":"E0DBNMQNDH",
                  "uid":"fohg4v1_dev1",
                  "flag":"2"
                });
                print("videoUrl:"+videoUrl);
                _controller = VideoPlayerController.network(videoUrl)
                  ..initialize().then((_) {
                    // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
                    setState(() {});
                  });
                setState(() {

                });
              } on PlatformException catch (err) {
                print("PlatformException");
                print(err.code);
                print(err.message);
                print(err.details);
              } catch (err) {
                // other types of Exceptions
                print("other err");
                print(err);
              }

            },child: Text("load video"),),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() {
              _controller.value.isPlaying
                  ? _controller.pause()
                  : _controller.play();
            });
          },
          child: _controller == null ? SizedBox() : Icon(
            _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
          ),
        ),

      ),
    );
  }
}
